mod middlewares;
mod stock;
mod trader;
mod vpn;

use away_db::model::appstate::AppState;
use axum::{middleware::*, routing::get, Router};
use sea_orm::DbConn;
use std::net::SocketAddr;
use tracing::*;
use trader::add_trader_route;
use vpn::add_vpn_router;

/// http 服务
pub fn http_server(host: String, appstate: AppState) {
    tokio::spawn(async move {
        info!("listening on http://{}", host);

        let app = Router::new()
            .route("/", get(|| async { "hello world" }))
            .merge(add_trader_route(appstate))
            .merge(add_vpn_router())
            .layer(from_fn(middlewares::cors::demo_middleware))
            .layer(from_fn(middlewares::auth::middleware));

        let addr = host.parse::<SocketAddr>().unwrap();
        axum::Server::bind(&addr)
            .serve(app.into_make_service())
            .await
            .unwrap();
    });
}

/// 后台任务
pub fn backgroup_service(db: DbConn) {
    trader::job::start(db.clone());
    stock::job::start(db.clone());
}
